package com.fire.control.system.module.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.config.redis.RedisService;
import com.fire.control.system.dto.AreaDto;
import com.fire.control.system.dto.CityDto;
import com.fire.control.system.dto.CountyDto;
import com.fire.control.system.dto.SelectDeviceGroupDTO;
import com.fire.control.system.dto.SelectGroupDTO;
import com.fire.control.system.dto.UserDTO;
import com.fire.control.system.module.common.dao.AreaMapper;
import com.fire.control.system.module.common.service.AreaService;
import com.fire.control.system.module.device.dao.DeviceGroupMapper;
import com.fire.control.system.statics.Key;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.common.SelectRequestProtocol;
import com.fire.control.system.support.util.XdaoDate;

/**
 * 
 * @ClassName:  NoticeServiceImpl
 * @Description:  消息通知服务接口具体实现
 * @author:  xiechengguang
 * @date:  2017年6月9日 下午7:00:44
 *
 */
@Service
public class AreaServiceImpl implements AreaService {
	@Autowired private ProtocolSelector protocolSelector;
	@Autowired private AreaMapper areaMapper;
	@Autowired private DeviceGroupMapper deviceGroupMapper; 
	@Autowired private RedisService redisService;
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: getRegion</p> 
	 * <p>Description: 获取省市县三级数据</p> 
	 * @return 
	 * @see com.fire.control.system.module.common.service.AreaService#getRegion()
	 */
	@Override
	public ResponseProtocol getRegion(SelectRequestProtocol request) {
		ResponseProtocol respProtocol = protocolSelector.choose(request);
		ResponseMessage.CODE_200.messageToProtocol(respProtocol);
		respProtocol.setDate(new XdaoDate());
		respProtocol.setData(getRegion());
		return respProtocol;
	}
	
	private List<AreaDto> getRegion(){
		List<AreaDto> areaList = redisService.getList(Key.redis_key_lora_region.toString(), AreaDto.class);
		if(areaList==null || areaList.isEmpty()){
			areaList = areaMapper.getProvinces();
			List<CountyDto> countyList = new ArrayList<CountyDto>();
			List<CityDto> cityList = new ArrayList<CityDto>();
			for(AreaDto pDto : areaList) {
				cityList = areaMapper.getCitys(pDto.getId());
				for(CityDto cityDto : cityList) {
					countyList = areaMapper.getCountys(cityDto.getId());
					cityDto.setCountys(countyList);
				}
				pDto.setCitys(cityList);
			}
			redisService.setList(Key.redis_key_lora_region.toString(), areaList, Key.redis_key_lora_region.getExpire());
		}
		return areaList;
	}
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: getRegionGroup</p> 
	 * <p>Description: 获取省市县三级数据  下面的设备对应的 设备组</p> 
	 * @param request
	 * @return 
	 * @see com.fire.control.system.module.common.service.AreaService#getRegionGroup(com.fire.control.system.support.protocol.common.SelectRequestProtocol)
	 */
	@Override
	public ResponseProtocol getRegionGroup(SelectRequestProtocol request, UserDTO user) {
		ResponseProtocol respProtocol = protocolSelector.choose(request);
		String companyId = user.getCompany().getId();
		
		List<AreaDto> areaList = redisService.getList(Key.redis_key_lora_region_device.toString(), AreaDto.class);
		if(areaList==null || areaList.isEmpty()){
			areaList = getRegion();
			for(AreaDto pDto : areaList) {
				for(CityDto cityDto : pDto.getCitys()) {
					for(CountyDto countyDto : cityDto.getCountys()) {
						String countyId = countyDto.getId();
						List<SelectDeviceGroupDTO> groupList = deviceGroupMapper.getCompanyAndCountyDeviceGroups(companyId, countyId);
						countyDto.setGroups(groupList);
					}
				}
			}
			redisService.setList(Key.redis_key_lora_region_device.toString(), areaList, Key.redis_key_lora_region_device.getExpire());
		}
		
		ResponseMessage.CODE_200.messageToProtocol(respProtocol);
		respProtocol.setDate(new XdaoDate());
		respProtocol.setData(areaList);
		return respProtocol;
	}
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: getGatewayGroup</p> 
	 * <p>Description: 获取省市县三级数据  下面的网关对应的 设备组</p> 
	 * @param request
	 * @param user
	 * @return 
	 * @see com.fire.control.system.module.common.service.AreaService#getGatewayGroup(com.fire.control.system.support.protocol.common.SelectRequestProtocol, com.fire.control.system.dto.UserDTO)
	 */
	@Override
	public ResponseProtocol getGatewayGroup(SelectRequestProtocol request, UserDTO user) {
		ResponseProtocol respProtocol = protocolSelector.choose(request);
		String companyId = user.getCompany().getId();
		
		List<AreaDto> areaList = redisService.getList(Key.redis_key_lora_region_gateway.toString(), AreaDto.class);
		if(areaList==null || areaList.isEmpty()){
			areaList = getRegion();
			for(AreaDto pDto : areaList) {
				for(CityDto cityDto : pDto.getCitys()) {
					for(CountyDto countyDto : cityDto.getCountys()) {
						String countyId = countyDto.getId();
						List<SelectDeviceGroupDTO> groupList = deviceGroupMapper.getCompanyAndCountyGatewayGroups(companyId, countyId);
						countyDto.setGroups(groupList);
					}
				}
			}
			redisService.setList(Key.redis_key_lora_region_gateway.toString(), areaList, Key.redis_key_lora_region_gateway.getExpire());
		}
		
		ResponseMessage.CODE_200.messageToProtocol(respProtocol);
		respProtocol.setDate(new XdaoDate());
		respProtocol.setData(areaList);
		return respProtocol;
	}
	
	
	
	
}
